<template>
  <div :class="['chat-history', isCollapse ? '' : 'history-hidden']">
    <div class="chat-history-robot">
      <div class="chat-history-robot-box">
        <img src="@/assets/images/labor/robot-ai.png" alt="" />
        <div class="chat-history-robot-content">
          <div class="chat-history-robot-title">AI 合规官</div>
          <div>专注于提供用工合规咨询</div>
        </div>
      </div>
      <div class="chat-history-robot-new">
        <el-tooltip effect="dark" content="新建对话" placement="top">
          <i class="el-icon-circle-plus-outline" @click="addRecords()"></i>
        </el-tooltip>
      </div>
    </div>
    <div class="chat-history-search">
      <el-input
        v-model="keyword"
        style="width: 328px"
        size="large"
        placeholder="搜索历史对话"
        suffix-icon="el-icon-search"
        @input="contentInput"
      />
    </div>
    <div class="chat-history-empty" v-if="historyList.length === 0">
      <img src="@/assets/images/labor/empty.png" alt="" />
      <div>无搜索结果</div>
    </div>
    <div class="chat-history-list">
      <!-- 今天 -->
      <div
        class="chat-history-list-label"
        v-if="
          historyList.filter((item) => isWhichDay(item.createTime) === '0')
            .length > 0
        "
      >
        今天
      </div>
      <div
        v-for="(item, index) in historyList.filter(
          (item) => isWhichDay(item.createTime) === '0'
        )"
        :key="item.id"
        :class="[
          'chat-history-list-row',
          item.id === historyId ? 'chat-history-active' : '',
        ]"
      >
        <div class="chat-history-list-text" @click="previewRecord(item)">
          <img src="@/assets/images/labor/default-news-ai.png" alt="" />
          <span>{{ item.title }}</span>
        </div>
        <el-popover
          v-if="dialogVisible"
          placement="bottom"
          :width="253"
          trigger="click"
          popper-class="chat-popover"
          v-model="item.visible"
          ref="popoverRef"
        >
          <div class="chat-history-popover">
            <div class="chat-history-tips">删除后无法恢复，是否继续删除？</div>
            <div class="chat-history-btn">
              <div @click="closePopover(item, index)">取消</div>
              <div
                @click="deleteRecord(item)"
                style="
                  background-color: #263b49;
                  border-color: #263b49;
                  color: #fff;
                  margin-left: 20px;
                "
              >
                删除
              </div>
            </div>
          </div>
          <template #reference>
            <div class="chat-history-list-delete">
              <img src="@/assets/images/labor/delete.png" alt="" />
            </div>
          </template>
        </el-popover>
      </div>
      <!-- 昨天 -->
      <div
        class="chat-history-list-label"
        v-if="
          historyList.filter((item) => isWhichDay(item.createTime) === '-1')
            .length > 0
        "
      >
        昨天
      </div>
      <div
        v-for="(item, index) in historyList.filter(
          (item) => isWhichDay(item.createTime) === '-1'
        )"
        :key="item.id"
        :class="[
          'chat-history-list-row',
          item.id === historyId ? 'chat-history-active' : '',
        ]"
      >
        <div class="chat-history-list-text" @click="previewRecord(item)">
          <img src="@/assets/images/labor/default-news-ai.png" alt="" />
          <span>{{ item.title }}</span>
        </div>
        <el-popover
          v-if="dialogVisible"
          placement="bottom"
          :width="253"
          trigger="click"
          popper-class="chat-popover"
          v-model="item.visible"
          ref="popoverRef"
        >
          <div class="chat-history-popover">
            <div class="chat-history-tips">删除后无法恢复，是否继续删除？</div>
            <div class="chat-history-btn">
              <div @click="closePopover(item, index)">取消</div>
              <div
                @click="deleteRecord(item)"
                style="
                  background-color: #263b49;
                  border-color: #263b49;
                  color: #fff;
                  margin-left: 20px;
                "
              >
                删除
              </div>
            </div>
          </div>
          <template #reference>
            <div class="chat-history-list-delete">
              <img src="@/assets/images/labor/delete.png" alt="" />
            </div>
          </template>
        </el-popover>
      </div>
      <!-- 更早 -->
      <div
        class="chat-history-list-label"
        v-if="
          historyList.filter(
            (item) => !['0', '-1'].includes(isWhichDay(item.createTime))
          ).length > 0
        "
      >
        更早
      </div>
      <div
        v-for="(item, index) in historyList.filter(
          (item) => !['0', '-1'].includes(isWhichDay(item.createTime))
        )"
        :key="item.id"
        :class="[
          'chat-history-list-row',
          item.id === historyId ? 'chat-history-active' : '',
        ]"
      >
        <div class="chat-history-list-text" @click="previewRecord(item)">
          <img src="@/assets/images/labor/default-news-ai.png" alt="" />
          <span>{{ item.title }}</span>
        </div>
        <el-popover
          v-if="dialogVisible"
          placement="top"
          :width="253"
          trigger="click"
          popper-class="chat-popover"
          v-model="item.visible"
          ref="popoverRef"
        >
          <div class="chat-history-popover">
            <div class="chat-history-tips">删除后无法恢复，是否继续删除？</div>
            <div class="chat-history-btn">
              <div @click="closePopover(item, index)">取消</div>
              <div
                @click="deleteRecord(item)"
                style="
                  background-color: #263b49;
                  border-color: #263b49;
                  color: #fff;
                  margin-left: 20px;
                "
              >
                删除
              </div>
            </div>
          </div>
          <template #reference>
            <div class="chat-history-list-delete">
              <img src="@/assets/images/labor/delete.png" alt="" />
            </div>
          </template>
        </el-popover>
      </div>
    </div>
  </div>
</template>

<script>
import moment from "moment";
export default {
  name: "GptHistory",
  props: {
    records: {
      type: Array,
      default: () => [],
    },
    isLoading: {
      type: Boolean,
      default: false,
    },
    historyId: {
      type: String,
      default: "",
    },
    isCollapse: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    records(val) {
      this.historyList = val;
    },
  },
  data() {
    return {
      historyList: [],
      keyword: "",

      dialogVisible: true,
    };
  },
  methods: {
    vueDebounce(time) {
      let timeout = null;
      return function () {
        if (timeout) {
          clearTimeout(timeout);
        }
        timeout = setTimeout(() => {
          this.$emit("contentEmit", this.keyword);
        }, time);
      };
    },
    contentInput() {
      let timeout = null;
      return function () {
        if (timeout) {
          clearTimeout(timeout);
        }
        timeout = setTimeout(() => {
          this.$emit("contentEmit", this.keyword);
        }, 500);
      };
    },
    addRecords() {
      if (this.isLoading) return;
      this.$emit("addEmit");
    },
    previewRecord(row) {
      if (this.isLoading) return;
      this.$emit("previewEmit", row);
    },
    deleteRecord(row) {
      if (this.isLoading) return;
      this.$emit("deleteEmit", row);
    },
    closePopover(row, index) {
      if (this.isLoading) return;
      this.dialogVisible = false;
      this.$nextTick(() => {
        this.dialogVisible = true;
      });
      this.$ref.popoverRef[index].hide();
    },
    isWhichDay(str) {
      str = moment(str).format("YYYY-MM-DD HH:mm:ss");
      const date = new Date();
      const that = moment(moment(str).format("YYYY-MM-DD")).valueOf();
      const today = moment(moment(date).format("YYYY-MM-DD")).valueOf();
      const timeStampDiff = that - today;
      const obj = {
        "-86400000": "-1",
        0: "0",
        86400000: "1",
      };
      return obj[timeStampDiff] || null;
    },
  },
};
</script>

<style lang="less" scoped>
.chat-history {
  display: flex;
  flex-direction: column;
  width: 300px;
  flex-shrink: 0;
  padding: 10px 0px;
  background: #fafafa;
  opacity: 1;
  overflow: hidden;
  transition: all 0.5s ease;
  .chat-history-robot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .chat-history-robot-box {
      display: flex;
      align-items: center;
      > img {
        width: 89px;
        flex-shrink: 0;
      }
      .chat-history-robot-content {
        flex: 1;
        flex-shrink: 0;
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 400;
        line-height: 19.6px;
        color: #565e66;
        margin-left: -10px;
        > div {
          width: 160px;
        }
      }
      .chat-history-robot-title {
        font-family: PingFang SC;
        font-size: 18px;
        font-weight: 600;
        line-height: 25.2px;
        color: #0e1114;
      }
    }
    .chat-history-robot-new {
      display: flex;
      align-items: center;
      margin: 0 20px 0 10px;
      cursor: pointer;
      > i {
        font-size: 16px;
      }
    }
  }
  .chat-history-search {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 20px 20px;
    :deep(.el-input__wrapper) {
      height: 46px;
      line-height: 46px;
      border-radius: 10px;
      box-shadow: 0px 6px 30px 5px rgba(0, 0, 0, 0.05);
    }
    :deep(.el-input__icon) {
      font-size: 16px;
      color: #535b63;
    }
  }
  .chat-history-empty {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 100px 0;
    > img {
      width: 160px;
      margin-bottom: 19px;
    }
    > div {
      font-family: PingFang SC;
      font-size: 14px;
      font-weight: 400;
      line-height: 19.6px;
      color: #333;
    }
  }
  .chat-history-list {
    flex: 1;
    overflow-y: scroll;
    padding: 0 20px;
    .chat-history-list-label {
      font-family: PingFang SC;
      font-size: 14px;
      font-weight: 400;
      line-height: 19.6px;
      color: #83898f;
      margin: 0 0 5px 12px;
    }
    .chat-history-list-row {
      position: relative;
      margin: 5px;
      &:hover {
        .chat-history-list-text {
          background-color: #ffffff;
          border-radius: 10px;
        }
        .chat-history-list-delete {
          display: inline-flex;
        }
      }
    }
    .chat-history-list-text {
      display: flex;
      align-items: center;
      cursor: pointer;
      height: 38px;
      padding: 12px;
      > img {
        width: 14px;
        margin-right: 5px;
      }
      > span {
        flex: 1;
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 400;
        line-height: 19.6px;
        color: #333;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        overflow: hidden;
      }
    }
    .chat-history-list-delete {
      position: absolute;
      top: 0;
      right: 0;
      display: none;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      margin-left: 5px;
      width: 28px;
      height: 100%;
      background-color: #fff;
      border-radius: 0 10px 10px 0;
      cursor: pointer;
      > img {
        width: 16px;
      }
    }
  }
  .chat-history-active {
    background-color: #fff;
    border-radius: 10px;
  }
}
.history-hidden {
  width: 0px;
  opacity: 0;
}
.chat-popover {
  .chat-history-popover {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px 0;
  }
  .chat-history-tips {
    font-family: PingFang SC;
    font-size: 14px;
    font-weight: 500;
    line-height: 22px;
    color: #000;
    margin-bottom: 19px;
  }
  .chat-history-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    > div {
      width: 70px;
      height: 28px;
      line-height: 28px;
      text-align: center;
      border: 1px solid rgba(199, 201, 203, 1);
      border-radius: 22px;
      font-family: PingFang SC;
      font-size: 14px;
      font-weight: 500;
      line-height: 28px;
      color: #263b49;
      cursor: pointer;
    }
  }
}
</style>
